package com.jwsoft.manager.core.dao.service.impl;

import cn.hutool.core.convert.Convert;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.enums.PersonAuthUseTypeEnum;
import com.jwsoft.manager.common.vo.personInformationAuth.PersonInformationAuthVO;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollPre;
import com.jwsoft.manager.core.dao.model.PersonInformationAuth;
import com.jwsoft.manager.core.dao.mapper.PersonInformationAuthMapper;
import com.jwsoft.manager.core.dao.model.PersonInformationSetting;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.jwsoft.manager.core.dao.service.PersonInformationAuthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwsoft.manager.core.dao.service.PersonInformationSettingService;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 个人信息授权表服务实现类
 *
 * @author chenzh
 * @since 2024-03-16
 */
@Service
public class PersonInformationAuthServiceImpl extends ServiceImpl<PersonInformationAuthMapper, PersonInformationAuth> implements PersonInformationAuthService {
    @Autowired
    private PersonInformationSettingService personInformationSettingService;
    @Autowired
    private EduStudentEnrollPreService eduStudentEnrollPreService;
    @Autowired
    private EduHelper eduHelper;
    @Override
    @Cached(name = EduCache.EDU_PERSON_INFOR_AUTH, key = "#idcard", cacheType = CacheType.BOTH, expire = 259200, localExpire = 100)
    public List<PersonInformationAuthVO> getByIdcard(String idcard) {
       return this.getByIdcardNoCache(idcard);
    }

    @Override
    public List<PersonInformationAuthVO> getByIdcardNoCache(String idcard) {
        LambdaQueryWrapper<PersonInformationSetting> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(PersonInformationSetting::getPersonInformationType,PersonInformationSetting::getPersonInformationName).eq(PersonInformationSetting::getUseType, PersonAuthUseTypeEnum.xsbm.getType())
                .eq(PersonInformationSetting::getStatus, BoolEnum.TRUE.getType());
        List<PersonInformationSetting> settingList=personInformationSettingService.list(queryWrapper);
        if (CollectionUtils.isEmpty(settingList)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PersonInformationAuth> authLambdaQueryWrapper=new LambdaQueryWrapper<>();
        authLambdaQueryWrapper.eq(PersonInformationAuth::getIdcard, idcard)
                .eq(PersonInformationAuth::getPersonInformationYear,eduHelper.thisYear());
        List<PersonInformationAuth> authList=this.list(authLambdaQueryWrapper);
        Map<String, PersonInformationAuth> map =new HashMap<>();
        if(!CollectionUtils.isEmpty(authList)){
            map=authList.stream().collect(Collectors.toMap(PersonInformationAuth::getPersonInformationType, a -> a,(k1, k2)->k1));
        }
        List<PersonInformationAuthVO> retList=new ArrayList<>();
        for(PersonInformationSetting setting:settingList){
            PersonInformationAuth auth=map.get(setting.getPersonInformationType());
            if (auth==null){
                PersonInformationAuthVO authVO=new PersonInformationAuthVO();
                authVO.setIdcard(idcard);
                authVO.setPersonInformationStatus(BoolEnum.FALSE.getType());
                authVO.setPersonInformationType(setting.getPersonInformationType());
                authVO.setPersonInformationName(setting.getPersonInformationName());
                retList.add(authVO);
            }else{
                PersonInformationAuthVO authVO= Convert.convert(PersonInformationAuthVO.class,auth);
                retList.add(authVO);
            }
        }
        return retList;
    }


    @Override
    public void clearCache(String idcard) {
        if(StringUtils.isBlank(idcard)){
            return;
        }
        Cache cache = JetCacheUtils.create(EduCache.EDU_PERSON_INFOR_AUTH, CacheType.BOTH);
        if (cache != null) {
            cache.remove(idcard);
        }
        Cache cacheCheck = JetCacheUtils.create(EduCache.EDU_PERSON_INFOR_AUTH_CHECK, CacheType.BOTH);
        if (cacheCheck != null) {
            cacheCheck.remove(idcard);
        }
    }


}
